package com.example.myapplication;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.dao.ConfirmDao;
import com.example.myapplication.entity.User;


public class LoginActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        setContentView(R.layout.login);

        getWindow().setStatusBarColor(Color.TRANSPARENT);
        ImageButton btngoback = findViewById(R.id.ibtn_back);
        btngoback.setOnClickListener(v -> finish());
        EditText account = findViewById(R.id.et_QQ);
        EditText passwd = findViewById(R.id.et_password);
        Button button = findViewById(R.id.btn_login);

        Handler handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.arg1 == 1) {
                    String username = ((User) msg.obj).getName();
                    ConfirmDao.username=username;
                    ConfirmDao.islogin=true;
                    Toast.makeText(getApplicationContext(), "欢迎" + username, Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(getApplicationContext(), "账号不存在或密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        };
        button.setOnClickListener(v -> new ConfirmDao().send(account.getText().toString(), passwd.getText().toString(), handler));
    }
}


